﻿package src.components 
{
	
	/**
	* 
	* Reliability project
	* -----------------------
	* @author  Dmitrij Boycov
	* @mail    dmitry.boycov@gmail.com
	* @date    April 27, 2010
	* @version 1
	* 
	*/
	
	// controls
	import fl.controls.TextInput;
	import fl.controls.TextArea;
	import fl.controls.ComboBox;
	import fl.data.DataProvider;
	// standart
	import flash.text.TextFormat;	
	import flash.events.Event;
	import flash.utils.*;
	// src 
	import src.components.BForm;
	import src.components.TabsPanel;
	import src.computation.distributionLaw.*;
	import src.computation.Recovery;
	// astra
	import com.yahoo.astra.containers.formClasses.FormItem;
	import com.yahoo.astra.containers.formClasses.FormLayoutStyle;
	import com.yahoo.astra.layout.LayoutContainer;	
	import com.yahoo.astra.layout.modes.BoxLayout;
	import com.yahoo.astra.fl.controls.TabBar;
	
	public class ElForm extends BForm {
		
		protected var itemContainer:LayoutContainer;
		protected var distrlaw = null;
		protected var lawsComboBox:ComboBox;
		protected var lawsComboBoxFormItem:FormItem;
		protected var tabs;
		protected var distrLawPanel;
		protected var recoveryPanel;
		protected var recoveryContainer;
			
		protected var laws = new Array( 
			{ text:  "", className: null },			
			{ text:  "Экспоненциальный", className:"src.computation.distributionLaw.ExpLaw"},
			{ text: "Постоянный", className: "src.computation.distributionLaw.ConstLaw" }, 		
			{ text: "Нормальный", className: "src.computation.distributionLaw.NormLaw" } );
		
		public function ElForm(textHeader = "") {
			super((textHeader=="")?"Типовой элемент":textHeader);
			/// added for bug of getDefinitionByName
			var a:ExpLaw = null;
			var b:NormLaw = null;
			var c:ConstLaw = null;
			/////
		}
		
		public override function getData():Dictionary {
			return null;
		}
		
		public override function setData(dict: Dictionary) {				
		}
		
		protected function createDistrLawPanel() {
			//laws comboBox
			lawsComboBox = new ComboBox();
			lawsComboBox.width = 150;
			lawsComboBox.addEventListener(Event.CHANGE, changeHandler);
			
			var dp = new DataProvider();
			for (var i = 0; i < this.laws.length; i++ ) {
				dp.addItem( { label: laws[i].text} );
			}			
			lawsComboBox.dataProvider = dp;	
			
			lawsComboBoxFormItem = new FormItem("Закон распределения", lawsComboBox);
			lawsComboBoxFormItem.labelAlign = FormLayoutStyle.LEFT;
			lawsComboBoxFormItem.labelWidth = 120;
			
			var box : BoxLayout = new BoxLayout();			
			box.direction = "vertical";
			box.verticalGap = 10;		
			this.distrLawPanel = new LayoutContainer(box);		
			this.distrLawPanel.autoMask = false;
			this.distrLawPanel.addChild(lawsComboBoxFormItem);
		}
		
		
		protected override function initUI() {
	/*		nameInput = new TextInput();
			nameInput.width = 150;				
			
			description = new TextArea();			
			description.width = 150;
			description.height = 80;			
	
			this.createDistrLawPanel();		
			//
			this.recoveryPanel = new Recovery();
			var box : BoxLayout = new BoxLayout();
			box.direction = "horizontal";
			box.horizontalGap = 5;			
			this.recoveryContainer = new LayoutContainer(box);	
			this.recoveryContainer.addChild(this.recoveryPanel);
			
			this.tabs = new TabsPanel([ { label:"Закон распределения", panel:this.distrLawPanel },
										{label:"Востановление",panel:this.recoveryContainer}]);		
			// button
			this.createOkAndCancel();
	*/		
		}
		
		protected override function initData() {		
	/*		nameFormItem = new FormItem("Имя", nameInput);					
			nameFormItem.labelAlign = FormLayoutStyle.TOP;
			nameFormItem.labelWidth = 30;			
			
			descriptionFormItem = new FormItem("Описание", description);
			descriptionFormItem.labelAlign = FormLayoutStyle.TOP;
			descriptionFormItem.labelWidth = 50;					
			
			var box : BoxLayout = new BoxLayout();
			box.direction = "horizontal";
			box.horizontalGap = 5;			
			var nameDescContainer = new LayoutContainer(box);	
			nameDescContainer.autoMask = false;
			nameDescContainer.addChild(nameFormItem);
			nameDescContainer.addChild(descriptionFormItem);			
			//			
			if(this.distrlaw!=null){
				this.renderDisrtrLowForm(this.distrlaw);
			}			
			
			var buttons = new FormItem("", [this.okButton, this.cancelButton]);
			buttons.labelWidth = 80;	
			
			var generalBox : BoxLayout = new BoxLayout();
			generalBox.direction = "vertical";
			generalBox.verticalGap = 10;			
			this.itemContainer = new LayoutContainer(generalBox);	
	*///		itemContainer.autoMask = false;
	//		itemContainer.x = itemContainer.y = 30;						
			
	//		itemContainer.addChild(nameDescContainer);			
	//		itemContainer.addChild(this.tabs);
	//		itemContainer.addChild(buttons);			
	//		this.form.addChild(itemContainer);
		}
			
		protected function renderRecoveryPanel(p) {
			if (p != null) {
				this.recoveryContainer
				if(this.recoveryContainer.contains(this.recoveryPanel)){
					this.recoveryContainer.removeChild(this.recoveryPanel);
				}
				this.recoveryPanel = p;	
				this.recoveryContainer.addChild(this.recoveryPanel);
			}
		}
		
		/**
		 * renderDisrtrLowForm
		 * It add controls of distr law. And it delete controls of old law
		 * @param	form - loyuotContainer of distr. law. (Panel with controls)
		 */
		protected function renderDisrtrLowForm(form) {
			if (this.distrLawPanel != null) {
				if (this.distrLawPanel.numChildren > 1 ) {
					this.distrLawPanel.removeChildAt(1);
				}
				if (form != null) {
					var className = getQualifiedClassName(form);
					var pattern:RegExp = /::/; 
					className = className.replace(pattern, ".");					
					var index = 0;
					for (var i = 0; i < this.laws.length; i++ ) {						
						if (laws[i].className == className) {							
							index = i;
						}
					}
					if (index != this.lawsComboBox.selectedIndex) {
						this.lawsComboBox.selectedIndex = index;
					}					
					this.distrLawPanel.addChildAt(form, 1);
				}
			}
		}
		
		/**
		 * changeHandler
		 * It handle event of combobox for distr. law
		 * @param	event
		 */
		protected function changeHandler(event) {			
			var it = this.laws[this.lawsComboBox.selectedIndex];			
			if(it.className!=null){
				var ClassReference:Class = getDefinitionByName(it.className) as Class;
				this.distrlaw = new ClassReference();
				this.renderDisrtrLowForm(this.distrlaw);
			}else {
				this.distrlaw = null;
				this.renderDisrtrLowForm(null);
			}
		}
	
		
	}
	
}